package yueyong.salarymanagement.module.system.login;

import org.springframework.web.bind.annotation.*;
import yueyong.salarymanagement.common.anno.NoNeedLogin;
import yueyong.salarymanagement.common.anno.NoValidPrivilege;
import yueyong.salarymanagement.common.anno.OperateLog;
import yueyong.salarymanagement.common.domain.ResponseDTO;
import yueyong.salarymanagement.constant.SwaggerTagConst;
import yueyong.salarymanagement.module.system.employee.domain.dto.EmployeeLoginFormDTO;
import yueyong.salarymanagement.module.system.employee.domain.dto.EmployeeSubmitFormDTO;
import yueyong.salarymanagement.module.system.login.domain.KaptchaVO;
import yueyong.salarymanagement.module.system.login.domain.LoginDetailVO;
import yueyong.salarymanagement.module.system.login.domain.RequestTokenBO;
import yueyong.salarymanagement.util.SmartRequestTokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import static yueyong.salarymanagement.common.constant.ResponseCodeConst.MOBILE_PHONE_ERROR;

/**
 * 后台登录
 *
 * @author lidoudou
 * @date 2017年12月19日上午11:46:04
 */
@RestController
@Api(tags = {SwaggerTagConst.Admin.MANAGER_USER_LOGIN})
@OperateLog
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/session/login")
    @ApiOperation(value = "登录", notes = "登录")
    @NoNeedLogin
    public ResponseDTO<LoginDetailVO> login(@Valid @RequestBody EmployeeLoginFormDTO loginForm, HttpServletRequest request) {
        loginForm.setLoginName(String.valueOf(loginForm.getMobilePhone()));
        loginForm.setLoginPwd("xxx");
        return loginService.login(loginForm, request);
    }

    @PostMapping("/session/submit")
    @ApiOperation(value = "注册", notes = "注册")
    @NoNeedLogin
    public ResponseDTO<LoginDetailVO> submit(@Valid @RequestBody EmployeeSubmitFormDTO submitFormDTO, HttpServletRequest request) {

        submitFormDTO.setLoginName(String.valueOf(submitFormDTO.getMobilePhone()));
        submitFormDTO.setLoginPwd("xxx");

        return loginService.submit(submitFormDTO, request);
    }

    @GetMapping("/session/get")
    @ApiOperation(value = "获取session", notes = "获取session")
    @NoValidPrivilege
    public ResponseDTO<LoginDetailVO> getSession() {
        RequestTokenBO requestUser = SmartRequestTokenUtil.getRequestUser();
        return ResponseDTO.succData(loginService.getSession(requestUser));
    }

    @GetMapping("/session/logOut")
    @ApiOperation(value = "退出登陆", notes = "退出登陆")
    @NoValidPrivilege
    public ResponseDTO<Boolean> logOut() {
        RequestTokenBO requestToken = SmartRequestTokenUtil.getRequestUser();
        if (null == requestToken) {
            return ResponseDTO.wrap(LoginResponseCodeConst.LOGIN_ERROR);
        }
        return loginService.logoutByToken(requestToken);
    }

//    @GetMapping("/session/verificationCode")
//    @ApiOperation(value = "获取验证码", notes = "获取验证码")
//    @NoNeedLogin
//    public ResponseDTO<KaptchaVO> verificationCode() {
//        return loginService.verificationCode();
//    }

    @GetMapping("/session/getVerificationCode")
    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    @NoNeedLogin
    public ResponseDTO getVerificationCode(@RequestParam long mobilePhone) {
        if(loginService.sendVerifyCode(mobilePhone)) {
            return ResponseDTO.succ();
        }
        return ResponseDTO.wrap(MOBILE_PHONE_ERROR);
    }

}
